package transaction;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @author chaird
 * @create 2021-10-25 11:53
 */
public class SyncProducer {
  public static void main(String[] args) throws Exception {

    // 事务回查器
    TransactionCheckListenerImpl checkListener = new TransactionCheckListenerImpl();
    // 事务消息生产者
    TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name");
    // NameServer地址
    producer.setNamesrvAddr("localhost:9876");
    producer.setSendMsgTimeout(999999999);
    // 注册事务回查监听
    producer.setTransactionCheckListener(checkListener);

    // 本地事务执行器
    TransactionExecuterimpl executerimpl = new TransactionExecuterimpl();
    producer.start();
    for (int i = 0; i < 1; i++) {
      // Create a message instance, specifying topic, tag and message body.
      Message msg =
          new Message(
              "TopicTest" /* Topic */,
              "TagA" /* Tag */,
              ("" + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */);
      // Call send message to deliver message to one of brokers.
      SendResult sendResult = producer.sendMessageInTransaction(msg, executerimpl, null);
      System.out.printf("%s%n", sendResult);
    }
    // Shut down once the producer instance is not longer in use.
    producer.shutdown();
  }
}
